package com.yycx.module.bus.provider.handler.haiying.ackamount;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.hy.BusBankStatement;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.hy.BusHyorderReturnService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.file.provider.service.SysFileService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 回款认款单
 *
 * @author zyf
 */
@Component
public class RefundPaymentTab2PageListHandler implements PageInterceptor<BusPayment> {

    @Resource
    private BusCustomService customService;

    @Resource
    private TaskService taskService;

    @Resource
    private SysFileService fileService;

    @Resource
    private BusPaymentService paymentService;

    @Resource
    private BusHyorderReturnService returnService;

    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        String paymentNo = params.get("paymentNo");
        Long companyId = OpenHelper.getCompanyId();
        cq.select(BusPayment.class, "*");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.eq(true, "payment.amountType", AmountTypeEnum.CustomRefund.getCode());
        cq.eq(true, "payment.companyId", companyId);
        cq.like("payment.paymentNo", paymentNo);
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "payDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "payDate", endDate);
        cq.createJoin(BusBankStatement.class);
        cq.orderByDesc("payment.createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            Long paymentId = entityMap.getLong("paymentId");
            BigDecimal saleAmount=returnService.totalSaleAmount(paymentId);
            paymentService.updateSaleAmount(paymentId,saleAmount);
            Long customId = entityMap.getLong("customId");
            List<EntityMap> fileList = fileService.selectFileListByFileType(paymentId.toString(), BusPayment.class, "");
            entityMap.put("hkImage", fileList);
            if (FlymeUtils.isNotEmpty(customId)) {
                BusCustom custom = customService.getById(customId);
                if (FlymeUtils.isNotEmpty(custom)) {
                    entityMap.put("customName", custom.getCustomName());
                }
            }
            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }

}
